//创建路由对象设置路由守卫
import router from './router'
//引入进度条
//@ts-ignore
import Nprogress from 'nprogress';
import 'nprogress/nprogress.css';
import useUserStore from './store/user';
import pinia from '@/store'
let userStore = useUserStore(pinia)
Nprogress.configure({ showSpinner: false });
//添加路由守卫
let useStore = useUserStore();
//存储用户未登录可以访问路由得路径
let whiteList = ["/home", '/hospital/register', '/hospital/detail', '/hospital/notice', '/hospital/close', '/hospital/search'];
//前置守卫
router.beforeEach((to, from, next) => {
    //动态设置网页标题
    Nprogress.start();
    // document.title = `${to.meta.title}`

    //判断用户是否登录
    let token = userStore.UserInfo.token;
    if (token) {
        //用户已登录
        next();
    }
    else {
        //用户未登录
        if (whiteList.includes(to.path)) {
            next();
        } else {
            userStore.visable = true;
            next({ path: '/home', query: { redirect: to.fullPath } })

        }
    }
})

// 后置守卫
router.afterEach((to, from, next) => {
    Nprogress.done();
})